<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>编辑员工</title>
  <link rel="stylesheet" href="./static/css/common.css">
  <link rel="stylesheet" href="./static/css/layui.css" media="all">
  <style>
    .my-upload-file {
      display: flex;
      justify-content: center;
    }

    .my-form-left {
      padding-left: 20px;
    }
  </style>
</head>
<body>

<div class="layui-row">
  <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>员工编辑</legend>
  </fieldset>
  <div class="layui-col-md8 layui-col-xs12 my-form-left">
    <form class="layui-form layui-form-pane" action="">
      <input type="hidden" name="id" id="id">
      <div class="layui-form-item" style="display: none;">
        <div class="layui-input-block">
          <input type="hidden" id="img" name="img" lay-verify="required">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">员工姓名</label>
        <div class="layui-input-block">
          <input type="text" name="name" class="layui-input" id="name" lay-verify="required" autocomplete="off"
                 placeholder="请输入员工姓名">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">员工密码</label>
        <div class="layui-input-block">
          <input type="password" name="password" id="password" autocomplete="off" lay-verify="required"
                 placeholder="请输入员工密码"
                 class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">员工电话</label>
        <div class="layui-input-block">
          <input type="text" name="phone" id="phone" autocomplete="off" lay-verify="required" placeholder="请输入员工电话"
                 class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">员工角色</label>
        <div class="layui-input-block">
          <input type="hidden" id="roleName">
          <select name="roleName" lay-filter="aihao" lay-verify="required" placeholder="请选择员工角色">
            <option value="">请选择</option>
            <option value="管理员">管理员</option>
            <option value="普通员工">普通员工</option>
          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
          <input type="text" name="email" id="email" autocomplete="off" lay-verify="required" placeholder="请输入邮箱"
                 class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">部门</label>
        <div class="layui-input-block">
          <input type="hidden" id="departmentId">
          <select name="departmentId" lay-filter="aihao" lay-verify="required">

          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">入职时间</label>
        <div class="layui-input-block">
          <input type="text" name="enterDate" id="enterDate" lay-verify="required" autocomplete="off"
                 class="layui-input"
                 placeholder="yyyy-MM-dd HH:mm:ss">
        </div>
      </div>
      <div class="layui-form-item">
        <button class="layui-btn layui-btn-primary" type="reset">重置</button>
        <button class="layui-btn" lay-submit="" lay-filter="demo2">保存</button>
      </div>
    </form>
  </div>
  <div class="layui-col-md4 layui-col-xs12 my-upload-file">
    <div class="layui-upload-drag" id="test10">
      <i class="layui-icon"></i>
      <p>点击上传，或将文件拖拽到此处</p>
      <div class="layui-hide" id="uploadDemoView">
        <hr>
        <img src="" alt="上传成功后渲染" style="max-width: 196px">
      </div>
    </div>
  </div>
</div>


<script src="./static/layui.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<script>
  layui.use(['form', 'jquery', 'layer', 'laydate', 'upload'], () => {
    let form = layui.form,
      $ = layui.$,
      layer = layui.layer,
      laydate = layui.laydate,
      upload = layui.upload;

    //监听提交
    form.on('submit(demo2)', function (data) {
      console.log(data.field);
      console.log($("#img").val());
      if ($("#img").val().trim() == "") {

        swal("提示", "请上传头像", "warning");
        return;
      }
      $.post("/StaffManagement/employee/save", data.field, data => {
        if (data.code == 200) {
          swal("提示", data.message, "success").then(() => {
            let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
            parent.layer.close(index); //再执行关闭
          });
        }
      })
      return false;
    });

    //拖拽上传
    upload.render({
      elem: '#test10'
      , url: '/StaffManagement/file/upload' //改成您自己的上传接口
      , type: "images"
      , done: function (res) {
        if (res.code == 200) {
          swal("提示", res.message, "success").then(() => {
            layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data.url);
            $("#img").val(res.data.filename);
          });

        }
      }
    });

    laydate.render({
      elem: '#enterDate'
      , type: 'datetime'
    });

    $(() => {
      $.get("/StaffManagement/department/getAll", "", data => {
          console.log(data);
        if (data.code == 0) {
          let options = `<option value="">--请选择部门--</option>`;
          for (let temp of data.data) {
            options += `<option value="${temp.id}">${temp.departmentName}</option>`;
          }
          $("select[name='departmentId']").append($(options));
          setTimeout(() => {
            let img = $("#img").val();
            let departmentId = $("#departmentId").val();
            let roleName = $("#roleName").val();
            if (img) {
              layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', "http://localhost:8080/StaffManagement/static/image/" + img);
            }
            if (departmentId) {
              $("select[name='departmentId'] option[value='" + departmentId + "']").attr("selected", "selected");
            }
            if (roleName) {
              $("select[name='roleName'] option[value='" + roleName + "']").attr("selected", "selected");
            }
            form.render();
          }, 300);
        }
      })
    })
  });
</script>
</body>
</html>